<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output
        encoding="utf-8"
        indent="yes"
        media-type="text/xml"
        omit-xml-declaration="yes"
        standalone="no" />

    <xsl:param name="cmd" />

    <xsl:include href="./inc/App.xslt"/>
    <xsl:include href="./inc/Form.xslt"/>

    <xsl:template match="/">
        <div>
        <xsl:choose>
            <xsl:when test="/InternalMessage"><xsl:call-template name="app_message" /></xsl:when>

            <xsl:when test="($cmd='GetSegmentForEdit') or ($cmd='InsertSegment') or ($cmd='NewSegment') or ($cmd='SetSegment')"><xsl:call-template name="form" /></xsl:when>

            <xsl:otherwise>
                <h1>Unknown Input</h1>
                <p>Command: <xsl:value-of select="$cmd"/></p>
                <p>Server Response:</p>
                <p><textarea id="RawServerResponse"><xsl:copy-of select="/" /></textarea></p>
            </xsl:otherwise>
        </xsl:choose>
        </div>
    </xsl:template>

    <xsl:template name="form">

        <div class="Branch">
            <xsl:for-each select="//SegmentBranch/*">
                <xsl:choose>
                    <xsl:when test="local-name(.) = 'Document'">
            <a class="DocumentLink" href="#" id="{concat('item_',position(),'a_',./DocumentId)}" title="{./Title}">Document <xsl:value-of select="./DocumentShortName"/></a><span class="BranchDelimiter">/</span>
                    </xsl:when>
                    <xsl:when test="local-name(.) = 'Segment'">
            <a class="SegmentLink" href="#" id="{concat('item_',position(),'a_',./SegmentId)}" title="{./SegmentName}"><xsl:value-of select="./SegmentName"/></a><span class="BranchDelimiter">/</span>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </div>

        <xsl:choose>
            <xsl:when test="string-length(/ServerData/Segment/SegmentId) &gt; 0">
        <h1 class="DataEntryForm"><xsl:copy-of select="/ServerData/Segment/SegmentName/node()"/></h1>
            </xsl:when>
            <xsl:otherwise>
        <h1 class="DataEntryForm">New Segment</h1>
            </xsl:otherwise>
        </xsl:choose>

        <form action="#" class="DataEntryForm" id="SegmentForm">
            <xsl:variable name="nodes"><xsl:value-of select="(count(//ChildSegments/Segment[ParentSegmentId=/ServerData/Segment/SegmentId])) + (count(//ChildDocuments/Document))"/> Related Items</xsl:variable>

            <div class="CalendarBlock" id="CalendarBlock">&#160;</div>
            <div id="DataFormTabView" class="yui-navset">
                <ul class="yui-nav">
                    <li class="selected"><a class="TabAnchor" href="#ItemOne"><em>General</em></a></li>
                    <li><a class="TabAnchor" href="#ItemTwo"><em><xsl:value-of select="$nodes"/></em></a></li>
                </ul>
                <div class="yui-content">
                    <div id="ItemOne">
                        <xsl:variable name="segment_id" select="/ServerData/Segment/SegmentId"/>
                        <xsl:if test="string-length($segment_id) &gt; 0">
                            <div class="IdentifierBlock" title="{concat('Database Identifier: ',/ServerData/Segment/SegmentId)}"><xsl:value-of select="/ServerData/Segment/SegmentId" /></div>
                            <input id="SegmentId" type="hidden" value="{/ServerData/Segment/SegmentId}" />
                        </xsl:if>
                        <div class="FormRow" id="ParentSegmentIdUlDropDown">
                            <xsl:variable name="parent_segment_id" select="/ServerData/Segment/ParentSegmentId"/>
                            <input id="ParentSegmentId" type="hidden" value="{$parent_segment_id}" />
                            <label>Parent Segment:</label>
                            <div class="UlDropDownSelectedItemBlock">
                                <a href="#" id="ParentSegmentIdSelectedItem" title="Click to change value…">
                                <xsl:choose>
                                    <xsl:when test="string-length($parent_segment_id) &gt; 0"><xsl:value-of select="//Segments/Segment[SegmentId=$parent_segment_id]/SegmentName"/></xsl:when>
                                    <xsl:otherwise>[Not Specified]</xsl:otherwise>
                                </xsl:choose>
                                </a>
                            </div>
                            <div class="UlDropDownItemsBlock" id="ParentSegmentIdItemsBlock">
                                <div class="UlDropDownCommands">
                                    <a href="#" id="ParentSegmentIdRemove">Remove</a>&#160;&#160;&#160;
                                    <a href="#" id="ParentSegmentIdReset">Reset</a>&#160;&#160;&#160;
                                    <a href="#" id="ParentSegmentIdCancel">Cancel</a>&#160;&#160;&#160;
                                </div>
                                <ul class="UlDropDownItems" id="ParentSegmentIdItems">
                                    <xsl:for-each select="//Segments/Segment[not(ParentSegmentId)]">
                                        <xsl:sort select="./SegmentName" data-type="text" />

                                        <xsl:variable name="id" select="./SegmentId" />

                                        <xsl:choose>
                                            <xsl:when test="string(./SegmentId) = string($segment_id)">
                                                <li>
                                                    <span class="Selected"><xsl:value-of select="./SegmentName" /></span>
                                                    <xsl:call-template name="drop_down_segments">
                                                        <xsl:with-param name="parent_seg_id" select="$id" />
                                                        <xsl:with-param name="parent_seg_name" select="./SegmentName" />
                                                        <xsl:with-param name="selected_seg_id" select="$segment_id" />
                                                    </xsl:call-template>
                                                </li>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <li>
                                                    <a id="{concat('item_',position(),'_',./SegmentId)}"><xsl:value-of select="./SegmentName" /></a>
                                                    <xsl:call-template name="drop_down_segments">
                                                        <xsl:with-param name="parent_seg_id" select="$id" />
                                                        <xsl:with-param name="parent_seg_name" select="./SegmentName" />
                                                        <xsl:with-param name="selected_seg_id" select="$segment_id" />
                                                    </xsl:call-template>
                                                </li>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:for-each>
                                </ul>
                            </div>
                        </div>

                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Segment/SegmentName" /><xsl:with-param name="type" select="'textarea_node'" /><xsl:with-param name="caption" select="'Segment Name'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Segment/CreateDate"/><xsl:with-param name="caption" select="'Create Date'"/><xsl:with-param name="type" select="'datepicker'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Segment/IsActive" /><xsl:with-param name="caption" select="'Segment Active?'"/><xsl:with-param name="type" select="'checkbox'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Segment/SortOrdinal" /><xsl:with-param name="caption" select="'Sort Ordinal'"/><xsl:with-param name="maxlength" select="'4'"/></xsl:call-template>

                    </div>
                    <div id="ItemTwo">
                        <xsl:choose>
                        <xsl:when test="(count(//ChildSegments/Segment) &gt; 0) or (count(//ChildDocuments/Document) &gt; 0)">
                            <xsl:apply-templates select="//ChildSegments">
                                <xsl:with-param name="id" select="/ServerData/Segment/SegmentId" />
                            </xsl:apply-templates>
                            <xsl:apply-templates select="//ChildDocuments" />
                        </xsl:when>
                        <xsl:otherwise>No related items.</xsl:otherwise>
                        </xsl:choose>
                    </div>
                </div>
            </div>
            <div class="EditCommands">
                <button type="reset">Cancel</button>
                <xsl:if test="string-length(/ServerData/Segment/SegmentId) &gt; 0"><button id="DeleteSegment">Delete</button></xsl:if>
                <xsl:variable name="save_id">
                <xsl:choose>
                    <xsl:when test="string-length(/ServerData/Segment/SegmentId) &gt; 0">SaveSegment</xsl:when>
                    <xsl:otherwise>SaveNewSegment</xsl:otherwise>
                </xsl:choose>
                </xsl:variable>
                <button id="{$save_id}">Save</button>
            </div>
        </form>
    </xsl:template>

    <xsl:template match="ChildDocuments">
        <h3>Related Documents</h3>
        <ul id="ChildDocuments">
            <xsl:for-each select="Document">
                <xsl:sort select="./Title" />
            <li>
                <xsl:variable name="cssclass_doclink">
                    <xsl:choose>
                        <xsl:when test="number(./IsActive) = 0">DocumentLink_Inactive</xsl:when>
                        <xsl:otherwise>DocumentLink</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <a class="{$cssclass_doclink}" href="#" id="{concat('item_',position(),'_',./DocumentId)}">
                    <xsl:copy-of select="./Title/node()"/>
                    <br/> <xsl:if test="(string-length(./DocumentShortName) &gt; 0) and (string-length(./FileName) &gt; 0)">[<xsl:value-of select="./DocumentShortName"/>, <xsl:value-of select="./FileName"/>]</xsl:if>
                </a>
            </li>
            </xsl:for-each>
        </ul>
    </xsl:template>

    <xsl:template match="ChildSegments">
        <xsl:param name="id" />

        <xsl:if test="count(Segment[ParentSegmentId=$id]) &gt; 0">
        <h3>Related Segments</h3>
        <ul id="ChildSegments">
            <xsl:for-each select="Segment[ParentSegmentId=$id]">
                <xsl:sort select="./SegmentName" />
            <li>
                <a class="SegmentLink" href="#" id="{concat('item_',position(),'_',./SegmentId)}"><xsl:copy-of select="./SegmentName/node()"/></a>
            </li>
            </xsl:for-each>
        </ul>
        </xsl:if>
    </xsl:template>

    <xsl:template name="drop_down_segments">

        <xsl:param name="parent_seg_id" />
        <xsl:param name="parent_seg_name" />
        <xsl:param name="selected_seg_id" />

        <xsl:if test="//Segments/Segment[ParentSegmentId=$parent_seg_id]">
        <ul>
            <xsl:for-each select="//Segments/Segment[ParentSegmentId=$parent_seg_id]">
                <xsl:sort select="./SegmentName" />
                
            <xsl:choose>
                <xsl:when test="string(./SegmentId) = string($selected_seg_id)">
                    <li>
                        <xsl:if test="string-length($parent_seg_name) &gt; 0"><xsl:value-of select="$parent_seg_name"/>: </xsl:if><span class="Selected"><xsl:value-of select="./SegmentName" /></span>
                        <xsl:call-template name="drop_down_segments">
                            <xsl:with-param name="parent_seg_id" select="./SegmentId" />
                            <xsl:with-param name="selected_seg_id" select="$selected_seg_id" />
                        </xsl:call-template>
                    </li>
                </xsl:when>
                <xsl:otherwise>
                    <li>
                        <a id="{concat('item_',position(),'_',./SegmentId)}"><xsl:if test="string-length($parent_seg_name) &gt; 0"><xsl:value-of select="$parent_seg_name"/>: </xsl:if><xsl:copy-of select="./SegmentName/node()" /></a>
                        <xsl:call-template name="drop_down_segments">
                            <xsl:with-param name="parent_seg_id" select="./SegmentId" />
                            <xsl:with-param name="selected_seg_id" select="$selected_seg_id" />
                        </xsl:call-template>
                    </li>
                </xsl:otherwise>
            </xsl:choose>

            </xsl:for-each>
        </ul>
        </xsl:if>

    </xsl:template>

</xsl:stylesheet> 
